﻿using System;
using Wisdom.Core;

namespace Wisdom.Core
{
    /// <summary>
    /// 描述：异常处理配置器 接口
    /// </summary>
    public interface IExceptionHandlerConfigurator : IConfigSourceConfigurator { }

    /// <summary>
    /// 异常处理配置器 类
    /// </summary>
    public class ExceptionHandlerConfigurator : ConfigSourceConfigurator, IExceptionHandlerConfigurator
    {
        #region Private Fields
        private readonly Type exceptionType;
        private readonly Type exceptionHandlerType;
        private readonly ExceptionHandlingBehavior behavior;
        #endregion

        #region Ctor
        /// <summary>
        /// 构造函数：实始化一个 <c>ExceptionHandlerConfigurator</c> 类.
        /// </summary>
        /// <param name="context">配置上下文.</param>
        /// <param name="exceptionType">异常处理类型.</param>
        /// <param name="exceptionHandlerType">异常处理类型.</param>
        /// <param name="behavior">异常处理行为.</param>
        public ExceptionHandlerConfigurator(IConfigSourceConfigurator context, Type exceptionType, Type exceptionHandlerType, ExceptionHandlingBehavior behavior)
            : base(context)
        {
            this.exceptionType = exceptionType;
            this.exceptionHandlerType = exceptionHandlerType;
            this.behavior = behavior;
        }
        /// <summary>
        /// 实始化一个类 <c>ExceptionHandlerConfigurator</c> 实例.
        /// </summary>
        /// <param name="context">配置上下文.</param>
        /// <param name="exceptionType">异常处理类型.</param>
        /// <param name="exceptionHandlerType">异常处理行为.</param>
        public ExceptionHandlerConfigurator(IConfigSourceConfigurator context, Type exceptionType, Type exceptionHandlerType)
            : this(context, exceptionType, exceptionHandlerType, ExceptionHandlingBehavior.Direct)
        { }
        #endregion

        #region Protected Methods
        /// <summary>
        /// 配置容器
        /// </summary>
        /// <param name="container">配置容器.</param>
        /// <returns>已配置容器.</returns>
        protected override CodeConfigSource DoConfigure(CodeConfigSource container)
        {
            container.AddException(this.exceptionType, behavior);
            container.AddExceptionHandler(this.exceptionType, this.exceptionHandlerType);
            return container;
        }
        #endregion
    }
}